home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / serial / mdm-2.000 / mdm-2 / fgetsraw.c < prev    next >
C/C++ Source or Header  |  1993-10-10  |  4KB  |  135 lines

  1. /*************************************************************************
  2. Get String from a RAW TTY with timeout
  3. --------------------------------------------------------------------------
  4.  
  5.     Copyright (C) 1992  Anthony Rumble
  6.  
  7.     This program is free software; you can redistribute it and/or modify
  8.     it under the terms of the GNU General Public License as published by
  9.     the Free Software Foundation; either version 1, or
  10.     any later version.
  11.  
  12.     This program is distributed in the hope that it will be useful,
  13.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.     GNU General Public License for more details. <copying>
  16.  
  17.     You should have received a copy of the GNU General Public License
  18.     along with this program; if not, write to the Free Software
  19.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  
  21. --------------------------------------------------------------------------
  22. RCS Info
  23.  
  24. $Header: /home/smilie/bbs/modem/RCS/fgetsraw.c,v 1.5 1992/10/10 06:04:17 smilie Exp $
  25.  
  26. $Log: fgetsraw.c,v $
  27.  * Revision 1.5  1992/10/10  06:04:17  smilie
  28.  * fixed a date
  29.  *
  30.  * Revision 1.4  1992/10/09  13:39:37  smilie
  31.  * fixed some MORE warnings <phew>
  32.  *
  33.  * Revision 1.3  1992/10/09  13:37:50  smilie
  34.  * fixed some warnings
  35.  *
  36.  * Revision 1.2  1992/10/09  10:14:45  smilie
  37.  * added GPL message
  38.  *
  39.  * Revision 1.1  1992/10/09  10:10:14  smilie
  40.  * Initial revision
  41.  
  42. *************************************************************************/
  43.  
  44. /* Feature test switches */
  45. #define _POSIX_SOURCE 1
  46. #define _FGETSRAW_C
  47.  
  48. /* System Headers */
  49. #include <stdio.h>
  50. #include <termios.h>
  51. #include <unistd.h>
  52.  
  53. /* Local Headers */
  54.  
  55. /* Macros */
  56.  
  57. /* File scope variables */
  58.  
  59. static char fgetsraw_rcsid[] = "$Id: fgetsraw.c,v 1.5 1992/10/10 06:04:17 smilie Exp $";
  60. #define RCSID fgetsraw_rcsid
  61.  
  62. /* External variables */
  63.  
  64. /* External Functions */
  65.  
  66. /* Structures and unions */
  67.  
  68. /* Functions */
  69.  
  70. /*************************************************************************
  71.                 FGETSRAW
  72. --------------------------------------------------------------------------
  73. s = string
  74. len = max length of string
  75. fh = file handle
  76. to = time out
  77.  
  78. will return 1 if string was recieved ok
  79. will return 0 if timeout
  80.  
  81. *************************************************************************/
  82. int fgetsraw(char *s, int len, FILE *fh, int to)
  83. {
  84. struct termios t, told;        /* termio structs */
  85. int a;
  86. int ch;
  87. char *ss = s;
  88. /**/
  89. s[0] = 0;            /* Initialise String */
  90. tcgetattr(fileno(fh), &t);    /* Get TTY Attributes */
  91. tcgetattr(fileno(fh), &told);
  92. t.c_iflag |= (ICRNL);        /* Turn CR to NL mode on */
  93. t.c_lflag &= ~(ICANON|ISIG);    /* Turn off CANON processing and Signal handling */
  94. t.c_cc[VMIN] = 0;        /* Turn VMIN off */
  95. t.c_cc[VTIME] = (to * 10);    /* Set timeout */
  96. tcsetattr(fileno(fh), TCSANOW, &t);    /* Set TTY */
  97.  
  98. for (a=0; a<(len-1); a++)
  99.     {
  100.     if (!read(fileno(fh), (void *)&ch, 1))
  101.         ch = -1;
  102.     if (ch == -1)                    /* Timeout or error */
  103.         {
  104.         tcsetattr(fileno(fh), TCSANOW, &told);    /* Reset TTY */
  105.         fflush(fh);                /* Flush TTY */
  106.         return 0;
  107.         }
  108.     if ((char)ch == '\r')                /* Return on CR's */
  109.         {
  110.         *ss = 0;                /* Set current to 0 */
  111.         tcsetattr(fileno(fh), TCSANOW, &told);    /* Reset TTY */
  112.         fflush(fh);                /* Flush TTY */
  113.         return 1;
  114.         }
  115.     else
  116.     if ((char)ch == '\n')                /* Return on Newlines */
  117.         {
  118.         *ss = 0;                /* Set current to 0 */
  119.         tcsetattr(fileno(fh), TCSANOW, &told);    /* Reset TTY */
  120.         fflush(fh);                /* Flush TTY */
  121.         return 1;
  122.         }
  123.     else                        /* Valid Character */
  124.         {
  125.         *ss = (char)ch;    /* Add to end of string */
  126.         ss++;
  127.         }
  128.     }
  129. *ss = 0;                        /* Set end of string */
  130. tcsetattr(fileno(fh), TCSANOW, &told);            /* Reset TTY */
  131. fflush(fh);                        /* Flush TTY */
  132. return 1;
  133. }
  134.  
  135.